package com.softaria.spkiller.analyzer.dependency;

import java.util.Collection;

import com.softaria.spkiller.dependencies.DependencyGraph;
import com.softaria.spkiller.layers.LayerIdentifier;
import com.softaria.spkiller.layers.classificator.Classificator;
import com.softaria.spkiller.metadata.ClassIdentifier;

public class ProhibitedDependencyReporter {

	public void reportProblem(DependencyGraph dependencyGraph,
			Classificator classificator,
			Collection<ClassIdentifier> classesToCheck,
			Collection<? extends LayerIdentifier> layersAllowed,
			DependencyProblemCollector collector) {

		if (classesToCheck != null) {
			for (ClassIdentifier classID : classesToCheck) {

				checkRespectLayerConstarints(classID,
						dependencyGraph.getUsedClasses(classID), collector,
						classificator, layersAllowed);

			}
		}

	}

	private void checkRespectLayerConstarints(ClassIdentifier classID,
			Collection<ClassIdentifier> usedClasses,
			DependencyProblemCollector collector, Classificator classificator,
			Collection<? extends LayerIdentifier> layersAllowed) {

		LayerIdentifier layer = classificator.classificate(classID);

		if (layer == null) {
			collector.reportNoLayer(classID);
			return;
		}

		for (ClassIdentifier usedClassId : usedClasses) {

			LayerIdentifier usedClassLayer = classificator
					.classificate(usedClassId);
			if (usedClassLayer != null) {
				if (!layersAllowed.contains(usedClassLayer)) {
					collector.reportProhibitedDependency(classID, layer,
							usedClassId, usedClassLayer);
				}
			}

		}

	}

}
